//
//  CSGXInventoryCheckController.m
//  CarSalesMS_GCX
//
//  Created by Ssky on 2016/12/9.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "CSGXInventoryCheckController.h"
#import "CSGXCommodityPickerController.h"
#import "CSGXHomeSaleContentView.h"
#import "CSGXCheckPreviewController.h"

#import "TCommodityCategoryDAL.h"

//#import "TCommodityCategoryModel.h"


@interface CSGXInventoryCheckController () <GXScanViewControllerDelegate>

@property (nonatomic, weak) CSGXHomeSaleContentView *vCommPicker;

@property (nonatomic, strong) CSGXCommodityPickerController *commPickerVc;


/// Data of first tv
@property (nonatomic, strong) NSArray<TCommodityCategoryModel *> *InvCheckFirstModels;


//navBar
@property (nonatomic, strong) NSArray *leftItems;




@end

@implementation CSGXInventoryCheckController
@synthesize checkInventoryList = _checkInventoryList;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self loadDataForCommTypes];
    
    // The observer that scrolls the scrollView  and views detail data for commTv.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedTvCommodityNoteInvCheck:) name:@"tvCommodityCellAction" object:nil];
    
    self.commPickerVc.commFirstModels = self.InvCheckFirstModels;
}


- (void)viewWillAppear:(BOOL)animated {
    if (self.isReceivedEdit) {
        [self contentScrollViewScroll];
    }
}

- (void)viewDidDisappear:(BOOL)animated {
    self.isReceivedEdit = YES;
    
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


#pragma mark - Actions
- (void)receivedTvCommodityNoteInvCheck:(NSNotification *)note {
    //    NSLog(@"receivedNote");
    
    [self contentScrollViewScroll];
}

- (void)leftBarClick:(id)sender {
    
    
}

- (void)leftTitleItem:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

//
- (void)rightCommit:(id)sender {
    CSGXCheckPreviewController *checkPreviewVc = [[CSGXCheckPreviewController alloc] init];
    
    NSArray<TInventoryModel *> *inventotyCheckedList = [CSGXCommodityPickerBLL getCheckTypeInventoryList:self.checkInventoryList.copy];
    
    checkPreviewVc.checkPreviewInventoryList = inventotyCheckedList;
    [self.navigationController pushViewController:checkPreviewVc animated:YES];
}

- (void)rightScan:(id)sender {
    
    GXScanViewController * scanVc = [[GXScanViewController alloc] init];
    scanVc.scanDelegate = self;
    [self presentViewController:scanVc animated:YES completion:nil];
}

- (void)rightSearch:(id)sender {
    self.navigationItem.leftBarButtonItems = @[];
    
    
    //
    self.navigationItem.leftBarButtonItems = self.leftItems;
}


#pragma mark - methods
- (void)loadDataForCommTypes {
    self.InvCheckFirstModels = [TCommodityCategoryDAL loadCoCategoryModelFirstWithWarehouseNo:[CSUserDefaultsTool getWarehouseNo]]; //Only have caFirstTypes.
}

- (void)contentScrollViewScroll {
    //
    [self.vCommPicker setContentOffset:CGPointMake(2 * self.view.gx_Width / 3, 0) animated:YES];
}

#pragma mark - delegates
- (void)gxScanVc:(GXScanViewController *)scanVc backObject:(NSString *)backObject backObjectType:(BackObjectType)backObjectType {
    //
    if (backObjectType == kObjectTypeDefault) {
        NSLog(@"没扫到任何鸟……");
        return;
    }
    
    if (backObjectType == kObjectTypeString) { //字符串
        [self contentScrollViewScroll];
        
        self.commPickerVc.commodityNo = backObject;
        
    }else {
        UIAlertController * alertVc = [UIAlertController alertControllerWithTitle:@"商品扫描" message:@"您扫描的是一个飞往火星的网址，请重试！" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction * actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            // 取消
        }];
        [alertVc addAction:actionCancel];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self presentViewController:alertVc animated:YES completion:nil];
        });
    }
    
}


#pragma mark - setupUI
- (void)setupNavBar {
//    self.navigationController.navigationBar.barTintColor = [UIColor redColor];

//    UIImage *backImg = [[UIImage imageNamed:@"back.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:backImg style:UIBarButtonItemStylePlain target:self action:@selector(leftBarClick:)];
    
    self.title = @"库存盘点";
    
    UIBarButtonItem *leftTitleItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftTitleItem:)];
    
    self.navigationItem.leftBarButtonItems = self.leftItems = @[leftTitleItem];
    
    //
    UIBarButtonItem *rightCommit = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:0 target:self action:@selector(rightCommit:)];
//    UIBarButtonItem *rightScan = [[UIBarButtonItem alloc] initWithTitle:@"扫描" style:0 target:self action:@selector(rightScan:)];
    UIBarButtonItem *rightScan = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(rightScan:)];
    UIBarButtonItem *rightSearch = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(rightSearch:)];
    
    self.navigationItem.rightBarButtonItems = @[rightCommit, rightScan, rightSearch];
}

- (void)setupUI {
    [self setupNavBar];
    
    //vHeadLine
    UIView *vHeadLine = [[UIView alloc] init];
    [self.view addSubview:vHeadLine];
    
    vHeadLine.backgroundColor = [UIColor blackColor];
    
    [vHeadLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(self.view).mas_offset(0);
        make.height.mas_offset(2);
    }];

    //vCommPicker
    CSGXHomeSaleContentView *vCommPicker = [[CSGXHomeSaleContentView alloc] init];
    [self.view addSubview:vCommPicker];
    self.vCommPicker = vCommPicker;
    
    [vCommPicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(vHeadLine.mas_bottom).mas_offset(0);
        make.leading.bottom.trailing.equalTo(self.view).mas_offset(0);
    }];

    CSGXCommodityPickerController *commPickerVc = [CSGXCommodityPickerController commodityPickerVcWithControllerEnum:ViewControllerInventoryCheck];
    
    [self gx_addChildController:commPickerVc intoView:vCommPicker];
    self.commPickerVc = commPickerVc;
    
    [commPickerVc.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.bottom.trailing.equalTo(vCommPicker).mas_offset(0);
    }];
}


#pragma mark - setter & getter
- (void)setCheckInventoryList:(NSArray<TInventoryModel *> *)checkInventoryList {
    _checkInventoryList = checkInventoryList;
    
    self.commPickerVc.inventoryPreviewList = checkInventoryList.mutableCopy;
    
}

- (NSArray<TInventoryModel *> *)checkInventoryList {
    _checkInventoryList = self.commPickerVc.inventoryPreviewList;
    return _checkInventoryList;
}










@end
